<html>
<head>
<style type="text/css" media="all">
body {
	text-align:center;
}
#frame {
	width:800px;;
	margin-right:auto;
	margin-left:auto; 
	margin-top:10px;
	padding:0px;
	text-align:left;
}
#messages { margin: 10px; background-color: #ddd; font-family: "Verdana"; font-size: 10; text-align:left;}
#head { margin: 10px; background-color: #bbb; border-style: solid; border-width: 1px; border-color: #111; text-align: left;}
.message { background-color: #efefef; margin: 10px; border-style: solid; border-width: 1px; border-color: #111; text-align:left;}
#espionnage { margin: 10px; background-color: #cce; font-family: "Verdana"; font-size: 10; text-align:left;}
.espionnage { background-color: #efefef; margin: 10px; border-style: solid; border-width: 1px; border-color: #111; text-align:left; font-family: "Verdana"; font-size: 10;}
</style>
<script type="text/javascript">
function formSubmit() { document.getElementById("messagesForm").submit() }
function uniSubmit() { document.getElementById("uniForm").submit() }
</script>
</head>
<body>
<div id="frame">
<?php
$_dbversion=2;
print "<pre>";
// var_dump($_REQUEST);
print "</pre>";
// init
require_once('Config.php');
$conf = new Config;
$root =& $conf->parseConfig('~ogame/.ogamerc', 'IniCommented');
$settings=$root->toArray();
global $_section;
foreach ($settings['root'] as $_section => $value) {
	if (is_array($value)) { break;}
}
$_type=array('General' => 'General');
if (isset($_REQUEST['uni'])){
	$_section = $_REQUEST['uni'];
} 
if (isset($_REQUEST['type'])) {
	$_type=$_REQUEST['type'];
}
// Read config;

$_dbfile=$settings['root'][$_section]['DBfile'];
print '<div id="head">';
print '<form method="GET" action="'.$_SERVER['PHP_SELF'].'" id="uniForm">';
//var_dump($settings);
print "<span>";
print "<select name=\"uni\" onchange=\"uniSubmit()\">\n";
foreach ($settings['root'] as $key => $value) {
	if (is_array($value)) {
		print "<option value=\"$key\"";
		if ($key == $_section) { print ' selected '; }
		print ">";
		print $value['Login']." Univers: ".$value['Univers'];
		print "</option>";
	}
}
print "</select></span>\n";
print "<span>";
printf('<a href="%s" target="_new">Get into Ogame</a>',$settings['root'][$_section]['Session']);
print "</span>";
print '</form>';
print '</div>';
// Read messages;

print '<div id="messages">';
if (isset($_dbfile)  && ($db = new PDO("sqlite:$_dbfile"))) {
	$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
//	$result = $db->query("pragma user_version;");
//	var_dump($result);
//	if ($result && $result[0][0] == $_dbversion) {
		if (isset($_REQUEST['msgid'])){
			$_msgid=$_REQUEST['msgid'];
			$db->beginTransaction();
			foreach ($_msgid as $_id) {
				$db->exec("delete from messages where id=$_id");
			}
			$db->commit();
		}
		print '<form method="GET" action="'.$_SERVER['PHP_SELF'].'" id="messagesForm">';
		if ($result = $db->query('select distinct type from messages order by type')){
			print "<input type=\"hidden\" name=\"uni\" value=\"$_section\"/>\n";
			foreach ($result as $type){
				print htmlentities($type[0]);
				print "<input type=\"checkbox\" name=\"type[".htmlentities($type[0])."]\" value=\"".htmlentities($type[0])."\" onchange=\"formSubmit()\"";
				if (isset($_type["$type[0]"])) {
					print ' checked ';
				}
				print "/>\n&nbsp;";
				$i++;
			}
		}
		$query="select * from messages ";
		if ($_type) {
			$query.="where type=";
			$query.=implode(" or type=",array_map(array($db,'quote'),$_type));
		}
		$query.=" order by date desc limit 100";
		$result = $db->query($query);
	
		$i=0;
		while ($message = $result->fetch()) {
			print "<div class=\"message\">";
			print "<span><input type=\"checkbox\" name=\"msgid[$i]\" value=\"$message[id]\" onchange=\"formSubmit()\"/></span>";
			print '<span> '.$message['date'].' </span>'."\n";
			print '<span> '.$message['type'].' </span>'."\n";
			print '<span> '.implode("<br/>\n",explode("\n",htmlentities($message['message']))).' </span>'."\n";
			print "</div>";
			$i++;
		}
	//	print "<input type=\"submit\" name=\"delete\" value=\"Delete selected messages\" />\n";
	print '</form>';
	print '</div>';
	print '<div id="espionnage">';
	if($result = $db->query('select * from espionnage order by (met+cri+deut);')) {
		print '<table class="espionnage">';
/*		print '<tr>';
		if(isset($result[0])){
			foreach (array_keys($result[0]) as $key) {
				print '<th>'.$key.'</th>';
			}
		}
		print '</tr>';
		print "\n"; */
		while ($espionnage=$result->fetch(PDO::FETCH_ASSOC)) {
			print '<tr>';
			foreach ($espionnage as $value) {
				print '<td>';
				print $value;
				print '</td>';
			}
			print '</tr>';
			print "\n";
		}
		print '</table>';
	}
	print '</div>';
}
?>
</div>
</body>
</html>
